package com.ecoolex.union.pay.retailer.api.graphql;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.union.pay.retailer.entity.UnionPayCoupon;
import com.ecoolex.union.pay.retailer.pojo.ManulReceiveUnionPayCouponDTO;
import com.ecoolex.union.pay.retailer.pojo.ReceiveUnionPayCouponDTO;
import com.ecoolex.union.pay.retailer.ws.UnionPayCouponService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import java.math.BigDecimal;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "银联优惠券接口")
public class UnionPayCouponGraphql {

    @Reference
    UnionPayCouponService unionPayCouponService;

    /**
     * 查询用户银联优惠券列表
     *
     * @param user
     * @return
     */
    @QueryField(field = "userUnionPayCouponList", description = "用户银联优惠券列表")
    public Pagination<UnionPayCoupon> userUnionCouponList(@NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return unionPayCouponService.getCouponByUserId(user.userId(), page);
    }

    /**
     * 查询用户银联优惠券是否可用
     * @param source
     * @param user
     * @return
     */
    @TypeField(type = UnionPayCoupon.class,field = "canUsed",description = "能使用")
    public boolean canUsed(@Source UnionPayCoupon source, @NonNull BigDecimal realPrice,@NonNull String currencyType, ConsumerMemberAuthority user) {
        user.assertLogin();
        return unionPayCouponService.canUsed(source, realPrice, currencyType, user.userId());
    }

    /**
     * 用户领取银联优惠券
     * 用户领取银联优惠券
     * @param coupon
     * @param user
     */
    @MutationField(field = "receiveUnionPayCoupon", description = "用户领取银联优惠券")
    public Boolean receiveUnionPayCoupon(@NonNull ReceiveUnionPayCouponDTO coupon, ConsumerMemberAuthority user) {
        user.assertLogin();
       unionPayCouponService.receiveUnionPayCoupon(coupon, user.userId(), user.userName());
       return Boolean.TRUE;
    }

    /**
     * 用户（输入券码）领取银联优惠券
     * 用户领取银联优惠券
     * @param coupon
     * @param user
     */
    @MutationField(field = "manualReceiveUnionPayCoupon", description = "用户（输入券码）领取银联优惠券")
    public UnionPayCoupon manualReceiveUnionPayCoupon(@NonNull ManulReceiveUnionPayCouponDTO coupon, ConsumerMemberAuthority user) {
        user.assertLogin();
        return unionPayCouponService.manulReceiveUnionPayCoupon(coupon, user.userId(), user.userName());
    }

    /**
     * 查询用户银联优惠券可用性
     *
     * @param user
     * @return
     */
    @QueryField(field = "userUnionPayCouponCanUsed", description = "用户银联优惠券可用性")
    public boolean userUnionPayCouponCanUsed(@NonNull Integer couponId,@NonNull BigDecimal realPrice,@NonNull String currencyType, ConsumerMemberAuthority user) {
        user.assertLogin();
        return unionPayCouponService.canUsed(couponId, realPrice, currencyType, user.userId()).isPresent();
    }

}
